//
//  HomeViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/15.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "HomeViewController.h"
#import "AdvertisingViewController.h"   //轮播点击进入的页面
#import "HOTDetailsViewController.h"    //“全程”推荐详情页面
#import "MoreViewController.h"          //更多“全程”详情页面
#import "BWeddingViewController.h"      //精品婚礼详情页面
#import "BVideoViewController.h"        //精品视频详情页面

#import "HPBoutiqueCell.h"      //自定义精品视频和精品婚礼cell
#import "HotCell.h"             //自定义“全程”推荐的cell
#import "AdvertisingView.h"     //自定义轮播广告View
#import "SectionrHeaderView.h"  //自定义section头

#import "Advertising.h"         //轮播model类
#import "HomePageModel.h"       //“全程”推荐model
#import "BoutiqueModel.h"       //精品婚礼/视频model


@interface HomeViewController ()<PullTableViewDelegate, UITableViewDataSource, UITableViewDelegate>

///  创建tableView
@property (nonatomic, retain)CKTableView *tableView;
///  推荐的数据数组
@property (nonatomic, retain)NSMutableArray *hotData;
///  精品婚礼的数据数组
@property (nonatomic, retain)NSMutableArray *weddingData;
///  精品视频的数据数组
@property (nonatomic, retain)NSMutableArray *videoData;

@end

@implementation HomeViewController

/// 网络请求
- (void)netWorking
{
    /// 视频,婚礼目录请求获取
    NSString *jingpinURL = @"http://data.halobear.cn/mapi/index.php?act=indexnav";
    [NetHandler networkWithURL:jingpinURL completion:^(id result) {
        /// 精品婚礼数据
        NSArray *array = [result objectForKey:@"jxhl"];
        for (NSDictionary *dic in array) {
            BoutiqueModel *model = [[BoutiqueModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.weddingData addObject:model];
        }
        /// 精品视频数据
        NSArray *array1 = [result objectForKey:@"jxsp"];
        for (NSDictionary *dic in array1) {
            BoutiqueModel *model = [[BoutiqueModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.videoData addObject:model];
        }
        [self.tableView reloadData];
    }];
    
    /// “全程”请求,数据获取
    NSString *hotUrl = @"http://circle.halobear.cn/api/mobile/index.php?module=forumdisplay&version=3&fid=2&page=1&filter=digest&digest=1&orderby=lastpost";
    [NetHandler networkWithURL:hotUrl completion:^(id result) {
        NSDictionary *dic = [result objectForKey:@"Variables"];
        NSArray *array = [dic objectForKey:@"forum_threadlist"];
        for (NSDictionary *dic0 in array) {
            HomePageModel *model = [[HomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:dic0];
            [self.hotData addObject:model];
        }
        [self.tableView reloadData];
    }];
}
///  刷新方法
static NSInteger count = 2;
- (void)shuaxinNetWork
{
    NSString *hotUrl = [NSString stringWithFormat:@"http://circle.halobear.cn/api/mobile/index.php?module=forumdisplay&version=3&fid=2&page=%d&filter=digest&digest=1&orderby=lastpost", count];
    [NetHandler networkWithURL:hotUrl completion:^(id result) {
        NSDictionary *dic = [result objectForKey:@"Variables"];
        NSArray *array = [dic objectForKey:@"forum_threadlist"];
        for (NSDictionary *dic0 in array) {
            HomePageModel *model = [[HomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:dic0];
            [self.hotData addObject:model];
        }
        [self.tableView reloadData];
    }];
    count ++;
    
//    NSLog(@"%@",hotUrl);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //初始化数组
    self.weddingData = [[NSMutableArray alloc] init];
    self.videoData = [[NSMutableArray alloc] init];
    self.hotData = [[NSMutableArray alloc] init];
    //网络请求
    [self netWorking];
    
    [self createTableView];
    
    
}

//  创建视图
- (void)createTableView
{
    //  自定义表头视图
    AdvertisingView *adView = [[AdvertisingView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width / 2)];
    adView.AdvBlock = ^(Advertising *model){
        /// 表头跳转下级页面
        AdvertisingViewController *xiangPing = [[AdvertisingViewController alloc]init];
        xiangPing.imageStr = model.banner_logo;
        xiangPing.title = @"详情";
        [self.navigationController pushViewController:xiangPing animated:YES];
    };
    
    self.tableView = [[CKTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:(UITableViewStylePlain)];
    _tableView.pullDelegate = self;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.tableHeaderView = adView;
    
    [self.view addSubview:_tableView];
    
    
}
#pragma mark 加载刷新
- (void) refreshTable
{
    // 刷新代码放在这
    if (self.weddingData == nil || self.videoData == nil) {
        [self netWorking];
    }    
    
    self.tableView.pullLastRefreshDate = [NSDate date];
    self.tableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
    //  加载更多实现代码放在在这
    [self shuaxinNetWork];
    self.tableView.pullTableIsLoadingMore = NO;
}

#pragma mark -
#pragma mark - PullTableViewDelegate协议

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
///  section 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // 分区的高度
    switch (section) {
        case 0:
            return 25;
            break;
        case 1:
            return 25;
            break;
        case 2:
            return 25;
            break;
        default:
            return 0;
            break;
    }
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0://精选婚礼
            return 1;
            break;
        case 1://精选视频
            return 1;
            break;
        case 2://(幻熊)结婚圈
            return self.hotData.count;
            break;
        default:
            return 0;
            break;
    }
}
///  返回自定义sectionView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SectionrHeaderView *sectionView = [[SectionrHeaderView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 15)];
    sectionView.layer.cornerRadius = 4.f;
    switch (section) {
        case 0:
        {
            sectionView.title.text = @"精选婚礼";
        }
            break;
        case 1:
        {
            sectionView.title.text = @"精选视频";
        }
            break;
        case 2:
        {
            sectionView.title.text = @"推荐婚礼全程";
        }
            break;
            
        default:
            break;
    }
    sectionView.tiaoZhuanBlock = ^(NSString *sectionTitle){
        
        MoreViewController *more = [[MoreViewController alloc] init];
        
        [self.navigationController pushViewController:more animated:YES];
    
    };
    return sectionView;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0://精选婚礼
        {
            HPBoutiqueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HPBWedding"];
            if (!cell) {
                cell = [[HPBoutiqueCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HPBWedding"];
            }
            cell.dataArray = self.weddingData;
            cell.actionBlock = ^(BoutiqueModel *model){// block传值并跳转下级页面
                BWeddingViewController *BWVC = [[BWeddingViewController alloc] init];
                BWVC.title = model.cate_name;
                [self.navigationController pushViewController:BWVC animated:YES];
                
            };
            return cell;
        }
            break;
        case 1://精选视频
        {
            HPBoutiqueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HPBVideo"];
            if (!cell) {
                cell = [[HPBoutiqueCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HPBVideo"];
            }
            cell.dataArray = self.videoData;
            cell.actionBlock = ^(BoutiqueModel *model){// block传值并跳转下级页面
                BVideoViewController *BVVC = [[BVideoViewController alloc] init];
                BVVC.title = model.cate_name;
                [self.navigationController pushViewController:BVVC animated:YES];
            };
            return cell;
        }
            break;
        case 2://婚礼圈
        {
            HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HPHot"];
            if (!cell) {
                cell = [[HotCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HPHot"];
            }
            cell.model = self.hotData[indexPath.row];
            return cell;
        }
            break;
        default:
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"站位"];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"站位"];
            }
            return cell;
        }
            break;
    }
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0://精选婚礼模块高度
            return self.view.frame.size.width * 0.3;
            break;
        case 1://精选视频模块高度
            return self.view.frame.size.width * 0.3;
            break;
        case 2://婚礼圈
            return 180;
            break;
        default:
            return 10;
            break;
    }
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 2:
        {
            HomePageModel *model = self.hotData[indexPath.row];
            NSLog(@"点击了:%@", model.subject);
            
            HOTDetailsViewController *hotDetails = [[HOTDetailsViewController alloc]init];
            hotDetails.HPmodel = model;
            [self.navigationController pushViewController:hotDetails animated:YES];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark -

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
